<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Product_model extends CI_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    public function get_product_details($product_id)
    {
        $sql = "select * from products where product_id = ? and status = 1";
        $result = $this->db->query($sql,$product_id);

        return $result;          
    }
    public function get_product_images($product_id)
    {
        $sql = "select * from product_images where product_id = ? and status = 1";
        $result = $this->db->query($sql,$product_id);

        return $result;          
    }
    public function get_product($product_id)
    {
        
        $result = $this->db->where('product_id',$product_id)->get('products');
        return $result;        
    }
    
    
    public function get_products()
    {
        $this->db->limit(5);
        $result = $this->db->get('products');
        return $result;        
    } 
    
    public function get_slider_images()
    {
        $this->db->select('*');
        $this->db->from('slider_images');
        $this->db->where(array('slider_status' => '1'));
        $result = $this->db->get();
        return $result;
    }
    
    public function get_promo_images(){
        $this->db->limit(3);
        $this->db->select('*');
        $this->db->from('promo_images');
        $this->db->where(array('promo_status' => '1'));
        $result = $this->db->get();
        return $result;
    }
       
    public function add_product($category_id,$subcategory_id,$brand_id,$name,$model,$price,$stock)
    {
         $product = array(
           'category_id' => $category_id,
           'subcategory_id' => $subcategory_id,
           'brand_id' => $brand_id,
           'name' => $name,
           'model' => $model,
           'standart_price' => $price,
           'stock' => $stock
           
        );
        
        if($this->db->insert('products', $product))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    public function add_product_details($product_id,$general_info,$technical_info)
    {
        $product_details = array(
            'general_info' => $general_info,
            'technical_info' => $technical_info
        
        );
        $this->db->where('product_id',$product_id);
        if($this->db->insert('product_details',$product_details))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public function get_cart_id_products($cart_id)
    {
        $sql = "select * from cart_products where cart_id = ?";
        $result = $this->db->query($sql,$cart_id);
        if($result->num_rows() > 0)
        {
            
            foreach($result->result() as $row)
            {
                $data['product_id'] =   $row->product_id;
                $data['seller_id']  =   $row->seller_id;
                $data['quantitiy']  =   $row->quantitiy;
                $product_detail     =   $this->get_product_details($row->product_id)->row(0);
                $data['model']      =   $product_detail->model;
                $data['name']       =   $product_detail->name; 
                $data['cart_product_id']    = $row->cart_product_id;
                $seller_result = $this->db->where('seller_id',$row->seller_id)->get('seller');
                if($seller_result->num_rows() > 0)
                {
                    
                    $seller_detail              =   $seller_result->row(0);
                    $data['seller_user_id']     =   $seller_detail->user_id;
                    $data['seller_user_price']  =   $seller_detail->user_price;
                    $data['seller_stock']       =   $seller_detail->stock;
                    $data['seller_mod']         =   $seller_detail->mod;
                    $data['seller_kargo']       =   $seller_detail->kargo; // 0 satıcıya ait -  1 alıcıya ait
                    $data['seller_fiyat_araligi_id']    =   $seller_detail->fiyat_araligi_id;       
                }
                $products[] = $data;
                    
            }
            return $products;
        }
        else
        {
            return false;
        }
    }




}
?>